package com.kmproject.haifu.mvp.presenter;

import android.app.Activity;
import android.app.Application;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.irozon.library.HideKey;
import com.jess.arms.base.App;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.CustomPopupWindow;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.HousePublishDuanZuPublishContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.MemberCenterUserInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoryBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserSfBean;
import com.kmproject.haifu.mvp.ui.adapter.PublicHouseXZDPItemAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/26/2019 18:09
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HousePublishDuanZuPublishPresenter extends BasePresenter<HousePublishDuanZuPublishContract.Model, HousePublishDuanZuPublishContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    String conetnt = "个人房东";
    String conetntYj = "押一";
    ImageView housepublic_dialog_yj_1_img, housepublic_dialog_yj_2_img, housepublic_dialog_yj_3_img;
    ImageView housepublic_dialog_lxrsf_grfd_img, housepublic_dialog_lxrsf_grzz_img, housepublic_dialog_lxrsf_zyfd_img, housepublic_dialog_lxrsf_jjr_img;
    private CustomPopupWindow customPopupWindowSelectStore;//选择归属店铺的view
    private PopuwindowSelectStoreViewHold popuwindowSelectStoreViewHold;

    @Inject
    public HousePublishDuanZuPublishPresenter(HousePublishDuanZuPublishContract.Model model, HousePublishDuanZuPublishContract.View rootView) {
        super(model, rootView);
    }
    /**
     * 获取详情根据id
     * @param houseId
     */
    public void viewHouseRent(String houseId)
    {
        mModel.viewHouseRent(Api.APP_DOMAIN+"/app/viewHouseRent/"+houseId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<HouseRentListShowInfoBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<HouseRentListShowInfoBean> houseRentListShowInfoBeanResultBean) {
                        if (AppUtil.isSuccess(houseRentListShowInfoBeanResultBean))
                        {
                            mRootView.viewHouseRentSuccess(houseRentListShowInfoBeanResultBean.getData());
                        }else {
                            mRootView.showMessage(houseRentListShowInfoBeanResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取店铺列表
     */
   public void querySelectStory()
   {
       Map<String,Object> maps=new HashMap<>();
       maps.put("page",1);
       maps.put("limit", AppConstant.pageSize_big);
       mModel.querySelectStory(maps).compose(RxUtils.applySchedulers(mRootView))
               .subscribe(new ErrorHandleSubscriber<ResultBean<List<MyStoryBean.ResultBean>>>(mErrorHandler) {
                   @Override
                   public void onNext(ResultBean<List<MyStoryBean.ResultBean>> listResultBean) {
                     if (AppUtil.isSuccess(listResultBean))
                     {
                         mRootView.querySelectStorySuccess(listResultBean.getData());
                     }else {
                       mRootView.showMessage(listResultBean.getMessage());
                     }
                   }
               });
   }

    /**
     * 选择店铺
     * @param activity
     * @param view
     * @param memberCenterUserInfoBean
     * @param resultBeans
     * @param callback
     */
    public void popuwindSelectStore(Activity activity, View view, MemberCenterUserInfoBean memberCenterUserInfoBean,List<MyStoryBean.ResultBean> resultBeans, SelectStoreCallback callback) {

        customPopupWindowSelectStore = new CustomPopupWindow.Builder().contentView(CustomPopupWindow.inflateView(activity, R.layout.housepublishpublish_selectstory)).parentView(view)
                .animationStyle(R.style.mypopwindow_anim_style).customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                    }
                }).isHaveBackGround(false).build();
        View view1 = customPopupWindowSelectStore.getContentView();
        popuwindowSelectStoreViewHold = new PopuwindowSelectStoreViewHold(view1);
        popuwindowSelectStoreViewHold.toolbarTitle.setText(ArmsUtils.getString(activity, R.string.housepublishpublish_title_xzgsdp));
        popuwindowSelectStoreViewHold.toolbarTitle.setTextColor(ArmsUtils.getColor(activity, R.color.color_191919));
        popuwindowSelectStoreViewHold.toolbar.setBackgroundColor(ArmsUtils.getColor(activity, R.color.color_EDEDED));
         popuwindowSelectStoreViewHold.toolbarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customPopupWindowSelectStore.dismiss();
            }
        });
        popuwindowSelectStoreViewHold.housepublishpublishSelectstoryRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
        PublicHouseXZDPItemAdapter adapter=new PublicHouseXZDPItemAdapter(memberCenterUserInfoBean,resultBeans);
        popuwindowSelectStoreViewHold.housepublishpublishSelectstoryRecyclerView.setAdapter(adapter);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                callback.callBack(resultBeans.get(position));
                customPopupWindowSelectStore.dismiss();
            }
        });
        customPopupWindowSelectStore.show();
    }

    /**
     * 获取用户身份
     */
    public void getUserShop() {
        mModel.getUserShop().compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<UserSfBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<UserSfBean> userSfBeanResultBean) {
                        if (AppUtil.isSuccess(userSfBeanResultBean)) {
                            mRootView.getUserShopSuccess(userSfBeanResultBean.getData());
                        }

                    }
                });
    }

    /**
     * 获取验证码
     *
     * @param phoneNum
     */
    public void sendHouseVerifyCode(String phoneNum) {
        mModel.sendHouseVerifyCode(phoneNum).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.showMessage("验证码已发送");
                            mRootView.sendHouseVerifyCodeSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }

                    }
                });
    }


    /**
     * 验证验证码对不对
     *
     * @param phoneNum
     * @param identifyCode
     */
    public void houseCheckIsCorrectCode(String phoneNum, String identifyCode) {
        mModel.houseCheckIsCorrectCode(phoneNum, identifyCode).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.showMessage("号码验证成功");
                            mRootView.houseCheckIsCorrectCodeSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }

                    }
                });
    }

    /**
     * 得到名字 字符串
     *
     * @param housePayWayBeans
     * @return
     */
    public List<String> getHousePayWay(List<HousePayWayBean> housePayWayBeans) {
        List<String> temName = new ArrayList<>();
        if (housePayWayBeans == null || housePayWayBeans.isEmpty()) {
            return temName;
        }
        for (HousePayWayBean housePayWayBean : housePayWayBeans) {
            temName.add(housePayWayBean.getName());
        }
        return temName;
    }

    /**
     * 获取区域信息
     *
     * @param value
     * @param housePayWayBeans
     */
    public void getHouseArea(String value, List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            mRootView.getHouseArea(housePayWayBeans);
            return;
        }
        mModel.getAreaTree(Api.APP_DOMAIN_ZIDIAN + "/app/baseArea/getAreaTree", value, "").compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getHouseArea(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取付款方式
     *
     * @param value
     * @param category
     */
    public void getHousePayWayBean(String value, String category, List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            mRootView.getHousePayWayBeanSuccess(housePayWayBeans);
            return;
        }//dictList4Category

        mModel.getHousePayWayBean(Api.APP_DOMAIN_ZIDIAN + "/app/dictList", value, category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getHousePayWayBeanSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }

                    }
                });
    }


    /**
     * 控制popu显示选择职业
     */
    public void showPopuWindow_selectYj(Activity activity, View view, String defaultData) {
        HideKey.initialize(activity, view);
        CustomPopupWindow customPopupWindow = new CustomPopupWindow.Builder().contentView(CustomPopupWindow.inflateView(activity, R.layout.housepublic_dialog_yj)).parentView(view)
                .animationStyle(R.style.mypopwindow_anim_style).customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                    }
                }).isHaveBackGround(false).build();
        View view1 = customPopupWindow.getContentView();
        TextView my_custom_title_cancel = view1.findViewById(R.id.my_custom_title_cancel);
        TextView my_custom_title_sure = view1.findViewById(R.id.my_custom_title_sure);
        my_custom_title_sure.setVisibility(View.INVISIBLE);
        housepublic_dialog_yj_1_img = view1.findViewById(R.id.housepublic_dialog_yj_1_img);
        housepublic_dialog_yj_2_img = view1.findViewById(R.id.housepublic_dialog_yj_2_img);
        housepublic_dialog_yj_3_img = view1.findViewById(R.id.housepublic_dialog_yj_3_img);
        showYj(defaultData);
        LinearLayout housepublic_dialog_yj_1_lay = view1.findViewById(R.id.housepublic_dialog_yj_1_lay);
        LinearLayout housepublic_dialog_yj_2_lay = view1.findViewById(R.id.housepublic_dialog_yj_2_lay);
        LinearLayout housepublic_dialog_yj_3_lay = view1.findViewById(R.id.housepublic_dialog_yj_3_lay);

        my_custom_title_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mRootView.showSf("","");
                customPopupWindow.dismiss();
            }
        });

        housepublic_dialog_yj_1_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conetntYj = "押一";
                showYj(conetntYj);
                mRootView.showSf("", conetnt);
                customPopupWindow.dismiss();
            }
        });
        housepublic_dialog_yj_2_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conetntYj = "押二";
                showYj(conetntYj);
                mRootView.showSf("", conetnt);
                customPopupWindow.dismiss();
            }
        });
        housepublic_dialog_yj_3_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conetntYj = "押三";
                showYj(conetntYj);
                mRootView.showSf("", conetnt);
                customPopupWindow.dismiss();
            }
        });
        customPopupWindow.show();
    }

    public void showYj(String yj) {

        switch (yj) {
            case "":
                housepublic_dialog_yj_1_img.setVisibility(View.VISIBLE);
                housepublic_dialog_yj_2_img.setVisibility(View.INVISIBLE);
                housepublic_dialog_yj_3_img.setVisibility(View.INVISIBLE);
                break;
            case "押一":
                housepublic_dialog_yj_1_img.setVisibility(View.VISIBLE);
                housepublic_dialog_yj_2_img.setVisibility(View.INVISIBLE);
                housepublic_dialog_yj_3_img.setVisibility(View.INVISIBLE);
                break;
            case "押二":
                housepublic_dialog_yj_1_img.setVisibility(View.INVISIBLE);
                housepublic_dialog_yj_2_img.setVisibility(View.VISIBLE);
                housepublic_dialog_yj_3_img.setVisibility(View.INVISIBLE);
                break;
            case "押三":
                housepublic_dialog_yj_1_img.setVisibility(View.INVISIBLE);
                housepublic_dialog_yj_2_img.setVisibility(View.INVISIBLE);
                housepublic_dialog_yj_3_img.setVisibility(View.VISIBLE);
                break;
        }
    }

    /**
     * 控制popu显示选择职业
     */
    public void showPopuWindow_selectZy(Activity activity, View view, String dafaultData) {
        HideKey.initialize(activity, view);
        CustomPopupWindow customPopupWindow = new CustomPopupWindow.Builder().contentView(CustomPopupWindow.inflateView(activity, R.layout.housepublic_dialog_lxrsf)).parentView(view).backgroundDrawable(activity.getResources().getDrawable(R.drawable.button_white_bg))
                .animationStyle(R.style.mypopwindow_anim_style).customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                    }
                }).isHaveBackGround(false).build();
        View view1 = customPopupWindow.getContentView();
        TextView my_custom_title_cancel = view1.findViewById(R.id.my_custom_title_cancel);
        TextView my_custom_title_sure = view1.findViewById(R.id.my_custom_title_sure);
        housepublic_dialog_lxrsf_grfd_img = view1.findViewById(R.id.housepublic_dialog_lxrsf_grfd_img);
        housepublic_dialog_lxrsf_grzz_img = view1.findViewById(R.id.housepublic_dialog_lxrsf_grzz_img);
        housepublic_dialog_lxrsf_zyfd_img = view1.findViewById(R.id.housepublic_dialog_lxrsf_zyfd_img);
        housepublic_dialog_lxrsf_jjr_img = view1.findViewById(R.id.housepublic_dialog_lxrsf_jjr_img);
        LinearLayout housepublic_dialog_lxrsf_grfd_lay = view1.findViewById(R.id.housepublic_dialog_lxrsf_grfd_lay);
        LinearLayout housepublic_dialog_lxrsf_grzz_lay = view1.findViewById(R.id.housepublic_dialog_lxrsf_grzz_lay);
        LinearLayout housepublic_dialog_lxrsf_zyfd_lay = view1.findViewById(R.id.housepublic_dialog_lxrsf_zyfd_lay);
        LinearLayout housepublic_dialog_lxrsf_jjr_lay = view1.findViewById(R.id.housepublic_dialog_lxrsf_jjr_lay);
        showzy(dafaultData);
        my_custom_title_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                customPopupWindow.dismiss();
            }
        });
        my_custom_title_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRootView.showSf("", conetnt);
                customPopupWindow.dismiss();
            }
        });
        housepublic_dialog_lxrsf_grfd_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conetnt = "个人房东";
                showzy(conetnt);
            }
        });
        housepublic_dialog_lxrsf_grzz_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conetnt = "个人转租";
                showzy(conetnt);
            }
        });
        housepublic_dialog_lxrsf_zyfd_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conetnt = "职业房东";
                showzy(conetnt);
            }
        });
        housepublic_dialog_lxrsf_jjr_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conetnt = "经纪人";
                showzy(conetnt);
            }
        });
        customPopupWindow.show();
    }

    public void showzy(String zy) {
        {
            switch (zy) {
                case "":
                    housepublic_dialog_lxrsf_grfd_img.setVisibility(View.VISIBLE);
                    housepublic_dialog_lxrsf_grzz_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_zyfd_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_jjr_img.setVisibility(View.INVISIBLE);
                    break;
                case "个人房东":
                    housepublic_dialog_lxrsf_grfd_img.setVisibility(View.VISIBLE);
                    housepublic_dialog_lxrsf_grzz_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_zyfd_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_jjr_img.setVisibility(View.INVISIBLE);
                    break;
                case "个人转租":
                    housepublic_dialog_lxrsf_grfd_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_grzz_img.setVisibility(View.VISIBLE);
                    housepublic_dialog_lxrsf_zyfd_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_jjr_img.setVisibility(View.INVISIBLE);
                    break;
                case "职业房东":
                    housepublic_dialog_lxrsf_grfd_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_grzz_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_zyfd_img.setVisibility(View.VISIBLE);
                    housepublic_dialog_lxrsf_jjr_img.setVisibility(View.INVISIBLE);
                    break;
                case "经纪人":
                    housepublic_dialog_lxrsf_grfd_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_grzz_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_zyfd_img.setVisibility(View.INVISIBLE);
                    housepublic_dialog_lxrsf_jjr_img.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    public class PopuwindowSelectStoreViewHold {

        @BindView(R.id.toolbar_backImg)
        ImageView toolbarBackImg;
        @BindView(R.id.toolbar_back)
        RelativeLayout toolbarBack;
        @BindView(R.id.toolbar_title)
        TextView toolbarTitle;
        @BindView(R.id.back_right_icon)
        ImageView backRightIcon;
        @BindView(R.id.back_right_txt)
        TextView backRightTxt;
        @BindView(R.id.toolbar_right)
        RelativeLayout toolbarRight;
        @BindView(R.id.toolbar)
        AutoToolbar toolbar;
        @BindView(R.id.housepublishpublish_selectstory_RecyclerView)
        RecyclerView housepublishpublishSelectstoryRecyclerView;

        public PopuwindowSelectStoreViewHold(View view) {
//            R.layout.housepublishpublish_selectstory
            ButterKnife.bind(this, view);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public  interface  SelectStoreCallback{
        void callBack(MyStoryBean.ResultBean resultBean);
    }
}
